# -*- coding: utf-8 -*-
# !/usr/bin/env python
from utils.mysql_utils import query_to_list

def query_soft_conf(soft_id=''):
    sql_parameter = []

    if soft_id:
        sql_fragment = "WHERE a.soft_id = %s"
        sql_parameter.append(soft_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT a.soft_id, a.conf_manage_ids, b.conf_manage_nick, b.conf_manage_name,b.conf_manage_comment,b.conf_manage_id
    FROM soft_management a
    LEFT JOIN conf_management b ON a.conf_manage_ids = b.conf_manage_ids AND b.status = 1
    %s
    AND a.status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data

def query_app_conf(app_id=''):
    sql_parameter = []

    if app_id:
        sql_fragment = "WHERE a.app_id = %s"
        sql_parameter.append(app_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT a.app_id, a.conf_manage_ids, b.conf_manage_nick, b.conf_manage_name,b.conf_manage_comment,b.conf_manage_id
    FROM apps_configuration a
    LEFT JOIN conf_management b ON a.conf_manage_ids = b.conf_manage_ids AND b.status = 1
    %s
    AND a.status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data

def query_conf(conf_manage_id=''):
    sql_parameter = []

    if conf_manage_id:
        sql_fragment = "WHERE a.conf_manage_id = %s"
        sql_parameter.append(conf_manage_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT *
    FROM conf_management a
    %s
    AND a.status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, True)

    return data
